import Vue from 'test'
import VueRouter from 'vue-router'
import childrenRoute from './childrenRoute'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'position',
        meta:{
            title:'定位中...',
            keepAlive:false
        },
        beforeEnter(to,from,next){
            if(window.localStorage.getItem('positionData')){
              next('/main');
              return;
            }
            next();
        },
        component: () => import(/* webpackChunkName: "position" */ '../views/position')
    },
    {
        path: '/login',
        name: 'login',
        meta:{
            title:'登录',
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/login')
    },
    {
        path: '/registry',
        name: 'registry',
        meta:{
            title:'注册',
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "registry" */ '../views/registry')
    },
    {
        path: '/main',
        name: 'main',
        meta:{
            title:'校园超市',
            keepAlive:true
        },
        component: () => import(/* webpackChunkName: "main" */ '../views/main'),
        children:childrenRoute
    },
    {
        path: '/detail',
        name: 'detail',
        meta:{
            title:'详情',
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "detail" */ '../views/detail')
    },
    {
        path: '/search',
        name: 'search',
        meta:{
            title:'搜索',
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "search" */ '../views/search')
    },
    {
        path: '/setAddress',
        name: 'setAddress',
        meta:{
            title:'设置地址',
            isAuthority:true,
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "setAddress" */ '../views/setAddress')
    },
    {
        path: '/setPos',
        name: 'setPos',
        meta:{
            title:'设置定位',
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "setPos" */ '../views/setPos')
    },
    {
        path: '/map',
        name: 'map',
        meta:{
            title:'选择地址',
            keepAlive:false
        },
        component: () => import(/* webpackChunkName: "map" */ '../views/map')
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
//1.全局守卫  2.路由独享内守卫   3.组件内守卫

router.beforeEach((to,from,next) => {
    //设置标题
    document.title = to.meta.title;
    //登录权限验证
    if(to.meta.isAuthority){
        console.log("=====isAuthority")
        //需要判断是否登录
        let token = localStorage.getItem('token') || '';
        if(token){
            next();
        }else{
            next({
                name:'login',
                query:{
                    preRoute:to.path
                }
            })
        }
    }else{
        next();
    }
})

export default router
